﻿using System;
using System.Collections.Generic;
using System.Text;
using KaiXin001er.UTIL;
using KaiXin001er.WebBrowser;
using System.Web;
using KaiXin001er.Objects;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace KaiXin001er.Logic
{
    public class Kaixin001Player:BasePlayer
    {
        
        #region BasePlayer 成员
        public Kaixin001Player(Account acct):base(acct)
        {
            
        }
        /// <summary>
        /// 继承登陆
        /// </summary>
        /// <returns></returns>
        public override bool SignOn()
        {
            
            dac.refreshWeb();
            bool 是否登陆成功 = false;
            string str首页 = dac.SimpleRequest(URLS.首页);
            //if (页面判断者.是首页吗(str首页))
            if(true)//不再判断是不是首页 总是登陆
            {
                RaiseMsg("[开心002] 我们每天也都在开心地进步着");
                请求参数 r = new 请求参数();
                r.add("url", "/home/");
                r.add("email", this.Account.AcctEmail);
                r.add("password", this.Account.AcctPassword);
                string str登录结果页 = dac.PostRequest(URLS.登陆请求页面, r.ToString());
                //判断是否需要输入验证码
                if (页面判断者.需要输入验证码登陆吗(str登录结果页))//如果需要验证码登陆那么就取一下图片显示出来让用户填写
                {
                    RaiseMsg("[开心002] 要求输入验证码");
                    FormInputCode inputCode = new FormInputCode();
                    if (inputCode.ShowDialog() == DialogResult.OK)
                    {
                        string codeStr = inputCode.ResultStr;
                        string randomStr = inputCode.RandomStr;
                        请求参数 r2 = new 请求参数();
                        r2.add("rcode", randomStr);
                        r2.add("url", "/home/");
                        r2.add("rpkey", "");
                        r2.add("invisible_mode", "0");
                        r2.add("email", this.Account.AcctEmail);
                        r2.add("password", this.Account.AcctPassword);
                        r2.add("code", codeStr);
                        //再次登陆
                        str登录结果页 = dac.PostRequest(URLS.登陆请求页面, r2.ToString());

                    }
                    else//没有输入验证而直接退出了
                    {

                        RaiseMsg("[开心002] 因为你没有输入验证码，退出本帐户的执行，不怪我");
                        return false;
                    }
                    
                    //要求输入验证码
                }
                if (页面判断者.登陆成功了吗(str登录结果页))
                {
                    //RaiseMsg("[Kaixin001] 登陆成功");
                    //读取我的开心网ID
                    if(!this.Account.FriendsLoaded)
                    this.Account.PlayerID = 页面分析者.ReadPlayerID(str登录结果页);
                    string ImageUrl = 页面分析者.ReadAccountImageURL(str登录结果页,this.Account.PlayerID);
                    if (!string.IsNullOrEmpty(ImageUrl)) 
                    {
                        Image image = dac.GetImage(ImageUrl, "");
                        //Image image = Image.FromFile(@"E:\120_171450_2.jpg");
                        if (image != null) this.Account.AcctImage = image;
                    }
                    RaiseMsg("[Kaixin002] "+this.Account.AcctName+"成功登陆,ID是"+this.Account.PlayerID);
                    IsLogin = true;
                    return true;

                }
                else
                {
                    return false;
                }
                
            }
            else
            {
                RaiseMsg("[开心002]首页登陆失败,开不了心了吧");
                return false;
            }

           

        }

        public override bool SignOff()
        {
            if (IsLogin)
            {
                //退出登录即可
                WebAccessor da= WebAccessor.Instance;
                dac.GetRequest(URLS.首页,"");
                string retString = da.Request(URLS.退出地址, "", WebAccessor.RequestMethod.GetMethod);
                IsLogin=false;
            }

            return !IsLogin;
        }



        public override bool PlayPlay()
        {
            
            //一个一个模块的启动东之
            //读取好友
            if (!this.Account.FriendsLoaded)
            {
                RaiseMsg("[Kaixin002] 好友好友");
                FriendPlayer fp = new FriendPlayer(Account);
                fp.OnMsg += new MsgDelegate(RaiseMsg);
                runPlayer(fp);
                this.Account.FriendsLoaded = true;
                Thread.Sleep(3000);
            }
            else
            {
                //朋友买马
                RaiseMsg("[Kaixin002] 朋友买卖");
                SlavePlayer cp = new SlavePlayer(Account);
                cp.OnMsg += new MsgDelegate(RaiseMsg);
                runPlayer(cp);
                //return true;
                //进入停车
                if (this.Account.IsParkGame)
                //if(false)
                {
                    RaiseMsg("[Kaixin002] 争车位");
                    Parker p = new Parker(Account);
                    p.OnMsg += new MsgDelegate(RaiseMsg);
                    runPlayer(p);
                }
                if (this.Account.IsHouseGame)
                //if(true)
                {
                    RaiseMsg("[Kaixin002] 卖房子");
                    FarmPlayer farmP = new FarmPlayer(Account);
                    farmP.OnMsg += new MsgDelegate(RaiseMsg);
                    runPlayer(farmP);
                }
            }
            //进入朋友买卖
            //进入花园
            RaiseMsg("[Kaixin002] "+this.Account.AcctName+"退出开心");
            RaiseMsg("\r\n");
            return true;
        }
        public void runPlayer(BasePlayer player)
        {
            if (player.SignOn()) player.PlayPlay();
            player.SignOff();
        }

        #endregion
    }
}
